import { Injectable } from '@nestjs/common';
import { User } from 'src/entities/user.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

@Injectable()
export class PositionService {
  constructor(
    @InjectRepository(User)
    private readonly userRepository: Repository<User>,
  ) {}

  async getAll() {
    return await this.userRepository
      .createQueryBuilder('user')
      .select([
        'trim(user.positionNameRu) as "positionRu"',
        'user.positionNameEn as "positionEn"',
      ])
      .where('user.positionNameRu IS NOT NULL')
      .andWhere('user.positionNameRu != :emptyVal', { emptyVal: '' })
      .andWhere('user.hideEmployee = :hideEmployee', { hideEmployee: false })
      .orderBy('trim(user.positionNameRu)', 'ASC')
      .distinct(true)
      .getRawMany();
  }
}
